﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.RFQ.Controls
{
    public partial class ModalRFQControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }
        public void ShowModal(int companyid, int rfqid)
        {
            ResetFields();
            if (rfqid > 0)
            {
                LoadRFQData(companyid, rfqid);
                hfRFQID.Value = rfqid.ToString();
                btnDelete.Visible = true;
            }
            mpeModalRFQControl.Show();
        }
        protected void btnModalOK_Click(Object sender, EventArgs e)
        {
            mpeModalRFQControl.Hide();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int RFQid = hfRFQID.Value == "" ? 0 : Convert.ToInt32(hfRFQID.Value);
                if (companyid > 0 && RFQid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQID", SqlDbType.Int, 0, ParameterDirection.Input, RFQid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblRFQ WHERE RFQID=@RFQID AND CompanyId=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);

                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    lblMessage.Text = "RFQ Deleted Successfully!";
                    btnSave.Visible = false;
                    btnDelete.Visible = false;
                }
            }
        }


        public void LoadRFQData(int companyid, int rfqid)
        {
            hfRFQID.Value = rfqid.ToString();
            if (rfqid > 0)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity();
                String sql = "SELECT RFQID,FirstName,LastName,MiddleName,Title,Mobile,Phone1,Phone2,Fax,Email,Street, " +
                                      "City,State,Zip,Country,Note,CompanyID FROM RSPRM.dbo.rs_tblRFQ " +
                                      "WHERE RFQID=@RFQID AND CompanyID=@CompanyID";

                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@CompanyID";
                param.Value = userIdentity.RSCompanyId;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@RFQID";
                param.Value = rfqid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtEmail.Text = myDataReader["Email"].ToString();
                    lblRFQID.Text = myDataReader["RFQID"].ToString();
                    txtOpenDate.Text = myDataReader["OpenDt"].ToString();
                    txtCloseDate.Text = myDataReader["BidCloseDt"].ToString();
                    txtContactName.Text = myDataReader["ContactName"].ToString();
                    txtMobile.Text = myDataReader["ContactMobile"].ToString();
                    txtOffice.Text = myDataReader["ContactOffice"].ToString();
                    ddlStatus.SelectedValue = myDataReader["Status"].ToString();
                    ddlIndustryType.SelectedValue = myDataReader["IndustryType"].ToString();
                    chkIsApproved.Checked = bool.Parse(myDataReader["IsApproved"].ToString());
                    txtSubject.Text = myDataReader["Subject"].ToString();
                    txtAdditionalInstruction.Text = myDataReader["AdditionalInstruction"].ToString();
                    txtTerm.Text = myDataReader["Term"].ToString();
                    txtDescription.Text = myDataReader["Description"].ToString();
                }
                myConnection.Close();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int RFQID = int.Parse(hfRFQID.Value);
                string OpenDate = txtOpenDate.Text;
                string CloseDate = txtCloseDate.Text;
                string ContactName = txtContactName.Text;
                string ContactMobile = txtMobile.Text;
                string ContactOffice = txtOffice.Text;
                string ContactEmail = txtEmail.Text;
                int Status = int.Parse(ddlStatus.SelectedValue);
                int IndustryType = int.Parse(ddlIndustryType.SelectedValue);
                string isApproved = chkIsApproved.Text;
                string Subject = txtSubject.Text;
                string AdditionalInstruction = txtAdditionalInstruction.Text;
                string Term = txtTerm.Text;
                string Description = txtDescription.Text;

                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsApproved", SqlDbType.Bit, 0, ParameterDirection.Input, isApproved);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Status", SqlDbType.Int, 0, ParameterDirection.Input, Status);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@IndustryType", SqlDbType.Int, 0, ParameterDirection.Input, IndustryType);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactName", SqlDbType.NVarChar, 50, ParameterDirection.Input, ContactName);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@OpenDate", SqlDbType.DateTime, 0, ParameterDirection.Input, Convert.ToDateTime(OpenDate));
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CloseDate", SqlDbType.DateTime, 0, ParameterDirection.Input, Convert.ToDateTime(CloseDate));
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactMobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, ContactMobile);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactOffice", SqlDbType.NVarChar, 15, ParameterDirection.Input, ContactOffice);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContactEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, ContactEmail);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Subject", SqlDbType.NVarChar, 250, ParameterDirection.Input, Subject);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@AdditionalInstruction", SqlDbType.NVarChar, -1, ParameterDirection.Input, AdditionalInstruction);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Term", SqlDbType.NVarChar, -1, ParameterDirection.Input, Term);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, -1, ParameterDirection.Input, Description);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQID", SqlDbType.Int, 0, ParameterDirection.Input, RFQID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 30, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveRFQ");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                lblMessage.Text = "RFQ Saved Successfully!";
                btnSave.Visible = false;
                btnDelete.Visible = false;
            }
        }

        private void ResetFields()
        {
            lblRFQID.Text = "";
            txtOpenDate.Text = "";
            txtCloseDate.Text = "";
            txtContactName.Text = "";
            txtMobile.Text = "";
            txtOffice.Text = "";
            txtEmail.Text = "";
            ddlStatus.SelectedValue = "0";
            ddlIndustryType.SelectedValue = "0";
            chkIsApproved.Checked = false;
            txtSubject.Text = "";
            txtAdditionalInstruction.Text = "";
            txtTerm.Text = "";
            txtDescription.Text = "";
            btnSave.Visible = true;
            btnDelete.Visible = false;
        }
    }
}